<!-- 商品管理 -->
<template>
  <el-container>
    <el-aside width="170px">
      <CategoryManager :config="categoryConfig" />
    </el-aside>
    <el-main class="base_main_content">
      <div class="base_screen_header">
        <span></span>
        <el-form :inline="true" class="base_search_form" @submit.prevent>
          <el-form-item>
            <el-select v-model="base" @change="searchBase" placeholder="请选择基地" clearable>
              <el-option v-for="(item, index) in baseOptions" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="name" @keyup[native].enter="searchName" @change="searchName" placeholder="商品名称" clearable
              :suffix-icon="Search" />
          </el-form-item>
        </el-form>
      </div>
      <StatusBar :data="statusList" :default="1" />
      <div class="base_main_header">
        <el-button type="primary" text @click="goodsExport">导出</el-button>
      </div>
      <GoodsTable type="manager" route="goodsmanager" @statusData="statusData" />
    </el-main>
  </el-container>
</template>

<script setup>
import GoodsTable from '@com/goods/GoodsTable.vue'
import CategoryManager from '@com/CategoryManager.vue';
import StatusBar from '@com/StatusBar.vue';
import { ref, reactive, watch } from 'vue';
import { Search } from '@element-plus/icons-vue'
import useRoute from "@util/router";
import $ from "@util/base"
import goodsConfigApi from "@api/goodsConfig"
import api from "@api/goods"

const route = useRoute()

const categoryConfig = {
  type: 'goods'
}

const base = ref(route.query().base || '')
const name = ref(route.query().name || '')

const baseOptions = ref([])

getConfig()

function getConfig() {
  goodsConfigApi.base(val => {
    baseOptions.value = val
  })
}

const statusList = reactive([{
  label: '全部',
  value: 1,
  total: 0
}, {
  label: '已上架',
  value: 3,
  total: 0
}, {
  label: '已下架',
  value: 2,
  total: 0
}])

function goodsExport() {
  let routeQuery = route.query()
  api.managerExport({
    categoryCode: routeQuery.c,
    shopCode: routeQuery.base,
    productName: routeQuery.name,
    state: routeQuery.status,
  }, val => {
    $.download(val.url, "商品列表导出")
  })
}

function download(href, title) {
  const a = document.createElement('a')
  a.setAttribute('href', href)
  a.setAttribute('download', title)
  a.click();
}

function statusData(val) {
  statusList.forEach((e, i) => { e.total = val[i] })
}

function searchBase(val) {
  route.replaceQuery('base', val)
}

function searchName(val) {
  route.replaceQuery('name', val)
}
</script>

<style lang="scss">

</style>